package com.yourenbang.lingyun.transaction.job.handler;

import cn.hutool.core.text.csv.CsvParser;
import cn.hutool.core.text.csv.CsvReadConfig;
import cn.hutool.core.text.csv.CsvRow;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayClient;
import com.alipay.api.domain.AlipayDataDataserviceBillDownloadurlQueryModel;
import com.alipay.api.request.AlipayDataDataserviceBillDownloadurlQueryRequest;
import com.alipay.api.response.AlipayDataDataserviceBillDownloadurlQueryResponse;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger;
import com.yourenbang.lingyun.constants.enums.pay.PayFromEnum;
import com.yourenbang.lingyun.core.util.KLog;
import com.yourenbang.lingyun.core.util.format.CharsetKit;
import com.yourenbang.lingyun.transaction.web.service.pay.PayTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.util.Iterator;
import java.util.zip.ZipInputStream;

@Component
@Slf4j
public class DownloadBillAliHandler {

    @Autowired
    private PayTypeService payTypeService;

    @XxlJob("downloadBillAliHandler")
    public ReturnT<String> downloadBillAliHandler(String param) throws Exception {
        XxlJobLogger.log("【下载支付宝订单】任务开始。。。");
        JSONObject requestParam = JSON.parseObject(param);
        AlipayClient alipayClient = payTypeService.getAlipayClient(PayFromEnum.getPayFrom(requestParam.getString("payFrom")));
        KLog.info("支付宝账单下载开始====");
        /**
         * 查询对账单下载地址
         * @param bizContent
         * @return
         * @throws AlipayApiException
         */
        AlipayDataDataserviceBillDownloadurlQueryModel model = new AlipayDataDataserviceBillDownloadurlQueryModel();
        AlipayDataDataserviceBillDownloadurlQueryRequest request = new AlipayDataDataserviceBillDownloadurlQueryRequest();

//        model.setBillType(Constant.DownloadBillAli.BILL_TYPE);
//        model.setBillType("trade");
        model.setBillType("signcustomer");

//        String yesterdayByCalendar = DateUtil.getYesterdayByCalendar(DateUtils.YYYY_MM_DD);
        String yesterdayByCalendar = "2020-05-08";
        KLog.info("下载的账单日期:{}", yesterdayByCalendar);
        model.setBillDate(yesterdayByCalendar);
        request.setBizModel(model);

        AlipayDataDataserviceBillDownloadurlQueryResponse response = alipayClient.execute(request);
        KLog.info("支付宝账单下载地址:{}", response.getBillDownloadUrl());
        URL url = new URL(response.getBillDownloadUrl());

        InputStream input = url.openStream();
        ZipInputStream zipIn = new ZipInputStream(input, CharsetKit.CHARSET_GBK);

        while ((zipIn.getNextEntry()) != null) {

            Reader isr = new InputStreamReader(zipIn, CharsetKit.CHARSET_GBK);

            CsvParser csv = new CsvParser(isr, new CsvReadConfig());
            CsvRow rows = csv.nextRow();
            int r = 1;
            while (rows != null) {
                KLog.info("当前读取第{}行", r++);

                Iterator<String> iterator = rows.iterator();
                int c = 1;
                while (iterator.hasNext()) {
                    String next = iterator.next();
                    KLog.info("csv读取的第{}列:{}", c++, next);
                }
                rows = csv.nextRow();
            }
            KLog.info("支付宝账单下载地址:{},返回体:{}", response.getBillDownloadUrl(), response.getBody());
        }
        return ReturnT.SUCCESS;

    }

}
